package breeze.lepei.der.utils;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import breeze.lepei.der.R;
import android.app.AlertDialog;
import android.graphics.Typeface;

public class MDialog extends AlertDialog{
    
	private static View dialig_view;

    private static MDialog m;

    private static TextView title2,content,positive_botton,negative_botton,navigate_button;

    public MDialog(Context context) {
        super(context);
        dialig_view=LayoutInflater.from(context).inflate(R.layout.dialog_view,null);
        content=dialig_view.findViewById(R.id.dialog_message);
        title2=dialig_view.findViewById(R.id.dialog_title);
        positive_botton=dialig_view.findViewById(R.id.dialog_button1);
        negative_botton=dialig_view.findViewById(R.id.dialog_button2);
        navigate_button=dialig_view.findViewById(R.id.dialog_button3);
		Typeface typeface=Typeface.createFromAsset(context.getAssets(),"font/chi.ttf");
		content.setTypeface(typeface);
		positive_botton.setTypeface(typeface);
		negative_botton.setTypeface(typeface);
		navigate_button.setTypeface(typeface);
		title2.setTypeface(typeface);
        this.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    public void setTitle(CharSequence title) {
        title2.setVisibility(View.VISIBLE);
        title2.setText(title);
    }

    public void setTitleTextColor(int color) {
        title2.setTextColor(color);
    }

    public void setTitleTextSize(int size) {
        title2.setTextSize(size);
    }

    @Override
    public void setMessage(CharSequence message) {
        content.setVisibility(View.VISIBLE);
        content.setText(message);
    }

    public void setContentTextColor(int color) {
        content.setTextColor(color);
    }

    public void setContentTextSize(int size) {
        content.setTextSize(size);
    }

    @Override
    public void show() {
        try {
            this.setView(dialig_view);
            super.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setPositiveButton(String text, final View.OnClickListener p) {
        positive_botton.setVisibility(View.VISIBLE);
        positive_botton.setText(text);
        if(p!=null){
			positive_botton.setOnClickListener(p);
		}else{
			positive_botton.setOnClickListener(new CloseOnClick());
		}
    }

    public void setNegativeBotton(String text, final View.OnClickListener p) {
        negative_botton.setVisibility(View.VISIBLE);
        negative_botton.setText(text);
        if(p!=null){
			negative_botton.setOnClickListener(p);
		}else{
			negative_botton.setOnClickListener(new CloseOnClick());
		}
    }

    public void setNavigateBotton(String text, final View.OnClickListener p) {
        navigate_button.setVisibility(View.VISIBLE);
        navigate_button.setText(text);
        if(p!=null){
			navigate_button.setOnClickListener(p);
		}else{
			navigate_button.setOnClickListener(new CloseOnClick());
		}
    }

    public void setPositiveColor(int color) {
        positive_botton.setTextColor(color);
    }

    public void setNegativeColor(int color) {
        negative_botton.setTextColor(color);
    }

    public void setNavigateColor(int color) {
        navigate_button.setTextColor(color);
    }

	public void setPositiveTextSize(int size) {
        positive_botton.setTextSize(size);
    }

    public void setNegativeTextSize(int size) {
        negative_botton.setTextSize(size);
    }

    public void setNavigateTextSize(int size) {
        navigate_button.setTextSize(size);
    }

	/**
	*@author BREEZE
	*@description 展示弹窗
	*@param Context，标题，内容
	*/
    public static void show(final Context context, final String title, final String content) {
        cancelDialog();
        m=new MDialog(context);
        if (title!=null) {
            m.setTitle(title);
        }
        m.setMessage(content);
        m.setPositiveButton("确定",null);
        m.show();
    }

    public static void cancelDialog() {
        if (m!=null) {
            m.cancel();
			m=null;
        }
    }
	
	/**
	*@author BREEZE
	*@description 关闭弹窗的点击事件
	*/
	class CloseOnClick implements View.OnClickListener {
		@Override
		public void onClick(View p1) {
			MDialog.this.dismiss();
		}
	}
	
    
}
